﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;

namespace CV_Website.Account
{
    public partial class Login : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "Logga in";
            RegisterHyperLink.NavigateUrl = "Register.aspx";
            //OpenAuthLogin.ReturnUrl = Request.QueryString["ReturnUrl"];

            var returnUrl = HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            if (!String.IsNullOrEmpty(returnUrl))
            {
                RegisterHyperLink.NavigateUrl += "?ReturnUrl=" + returnUrl;
            }
        }

        protected void Login_Click(object sender, EventArgs e)
        {
            string userName = LoginBox.UserName;
            string password = LoginBox.Password;
            DAL.Account account;

            if (DAL.AccountDB.AuthenticateLogin(userName, password, out account))
            {
                Session["Account"] = account;
                Response.Redirect("~\\Default.aspx");
            }
            else
            {
                LoginBox.FailureText = "Fel användarnamn eller lösenord.";
            }
        }
    }
}